package showhow.simple.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import showhow.simple.common.Result;
import showhow.simple.common.enums.ErrorCode;
import showhow.simple.model.convert.CustomerConvert;
import showhow.simple.model.entity.CustomerDO;
import showhow.simple.model.vo.CalculateVO;
import showhow.simple.model.vo.CustomerVO;
import showhow.simple.service.CustomerService;

@RestController
public class ShowHowController {
    @Autowired
    private CustomerService customerService;

    @RequestMapping("/")
    public String index() {
        return "OK";
    }

    @PostMapping("/login")
    public Result<CustomerVO> login(@RequestBody CustomerVO vo) {
        if(vo.getName() == null) {
            return Result.failure(ErrorCode.REQUET_PARAM_VERIFY_FAILURE);
        }
        CustomerDO customerDO = customerService.getByNameAndPaswd(vo.getName(), vo.getPaswd());
        if(customerDO != null) {
            // 模拟业务逻辑
            CustomerVO customerVO = CustomerConvert.INSTANCE.convertToVO(customerDO);
            return Result.success(customerVO);
        } else {
            return Result.failure(ErrorCode.PASSWORD_INCORRECT);
        }
    }

    @PostMapping("/calculateFee")
    public Result<Double> calculateFee(@RequestBody CalculateVO vo) {
        Double fee = customerService.calculateActualFee(vo.getId(), vo.getTotalFee());
        return Result.success(fee);
    }
}
